package com.xfly.app.ui;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.AllUtils;
import com.ptmall.app.utils.TimeUtil;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.bean.MzBean;
import com.xfly.app.bean.ZpBean;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class MzSetActivity extends BaseMvpActivity implements View.OnClickListener {

    TitleBarView title_bar;
    TextView time1,time2;

    PopupWindow popupWindow;
    private View popView;

    DateFormat format= DateFormat.getDateTimeInstance();
    Calendar calendar= Calendar.getInstance(Locale.CHINA);

    private TextView cancel;

    ImageView se1,se2,se3;

    String type ="1";

    EditText num;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    TextView mzmk,zpsl,hdsp,zp;
    LinearLayout mzlin;

    String mknum = "";
    Button btn;
    MzBean mzBean;

    String goodid,zpid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_mzset);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        time1 = findViewById(R.id.time1);
        time2 = findViewById(R.id.time2);
        mzlin = findViewById(R.id.mzlin);
        mzmk = findViewById(R.id.mzmk);
        zpsl = findViewById(R.id.zpsl);
        hdsp = findViewById(R.id.hdsp);
        zp = findViewById(R.id.zp);
        btn = findViewById(R.id.btn);

        title_bar.setText("满赠");
        title_bar.setRightText("修改");
    }

    @Override
    public void initEvents() {
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        time1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    // 绑定监听器(How the parent is notified that the date is set.)
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");


                        final Calendar calendar = Calendar.getInstance();
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        //实例化TimePickerDialog对象
                        final TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                            //选择完时间后会调用该回调函数
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                time1.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth+" "+hourOfDay+":"+minute);
                                //设置TextView显示最终选择的时间
//                                select_time.setText(hourOfDay + ":" + minute);
                            }
                        }, hour, minute, true);
                        timePickerDialog.show();

                    }
                }
                        // 设置初始日期
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        time2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    // 绑定监听器(How the parent is notified that the date is set.)
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");


                        final Calendar calendar = Calendar.getInstance();
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        //实例化TimePickerDialog对象
                        final TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                            //选择完时间后会调用该回调函数
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                time2.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth+" "+hourOfDay+":"+minute);
                                //设置TextView显示最终选择的时间
//                                select_time.setText(hourOfDay + ":" + minute);
                            }
                        }, hour, minute, true);
                        timePickerDialog.show();

                    }
                }
                        // 设置初始日期
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        mzlin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });
        hdsp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getContext(), KcGoodActivity.class);
                startActivityForResult(intent, 4);
            }
        });
        zp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),ZPGoodActivity.class);
                startActivity(intent);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        zpsl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),ZPSelectGoodActivity.class);
                startActivityForResult(intent, 3);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMz();
        getZpList();
    }

    private void pop() {
        if (null == popupWindow) {
            if (null == popView) {
                popView = LayoutInflater.from(this).inflate(R.layout.pop_mz, null);
                popView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                    }
                });
                cancel = popView.findViewById(R.id.cancel);
                num = popView.findViewById(R.id.num);
                se1 = popView.findViewById(R.id.se1);
                se2 = popView.findViewById(R.id.se2);
                se3 = popView.findViewById(R.id.se3);
                cancel.setOnClickListener(this);
                se1.setOnClickListener(this);
                se2.setOnClickListener(this);
                se3.setOnClickListener(this);
                popView.findViewById(R.id.pay).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(TextUtils.isEmpty(mknum)){
                            mzmk.setText("购买"+num.getText().toString()+"件活动商品即可参与");
                        }else{
                            mzmk.setText("购买"+mknum+"件活动商品即可参与");
                            if(mknum.equals("3")){
                                mzmk.setText("购买全部活动商品即可参与");
                            }
                        }
                        popupWindow.dismiss();
                    }
                });
                num.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        se1.setImageResource(R.drawable.xf_false);
                        se2.setImageResource(R.drawable.xf_false);
                        se3.setImageResource(R.drawable.xf_false);

                        mknum = "";
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                });
            }
//            View view = LayoutInflater.from(this).inflate(R.layout.personaldata_pop, null);
            popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setAnimationStyle(R.style.dialogAnim);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
            AllUtils.backgroundAlpha(0.5f, getWindow());
            popupWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    AllUtils.backgroundAlpha(1.0f, getWindow());
                }
            });
            //popupWindow.showAtLocation(rootView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        } else {
            AllUtils.backgroundAlpha(0.5f, getWindow());
            popupWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.cancel:
                popupWindow.dismiss();
                break;
            case R.id.se1:
                se1.setImageResource(R.drawable.xf_true);
                se2.setImageResource(R.drawable.xf_false);
                se3.setImageResource(R.drawable.xf_false);
                mknum = "1";
                break;
            case R.id.se2:
                se1.setImageResource(R.drawable.xf_false);
                se2.setImageResource(R.drawable.xf_true);
                se3.setImageResource(R.drawable.xf_false);
                mknum = "2";
                break;
            case R.id.se3:
                se1.setImageResource(R.drawable.xf_false);
                se2.setImageResource(R.drawable.xf_false);
                se3.setImageResource(R.drawable.xf_true);
                mknum = "3";
                break;
        }
    }

    private void getMz(){
        apiDataRepository.getmz(null, new ApiNetResponse<MzBean>() {
            @Override
            public void onSuccess(MzBean data) {
                mzBean =data;

                time1.setText(TimeUtil.getStrTime(data.getStime()+""));
                time2.setText(TimeUtil.getStrTime(data.getEtime()+""));
                mzmk.setText("购买"+data.getFull()+"件活动商品即可参与");
                zpsl.setText("1");
                hdsp.setText(data.getGoods().size()+"");

                zpid = data.getGift().getId()+"";
//                StringBuffer stringBuffer = new StringBuffer();
//                for(int i=0;i<data.getGoods().size();i++){
//                    stringBuffer.append(data.getGoods().get(i).get)
//                }
//                goodid = data.getGoods().
            }
        });
    }

    private void getZpList(){
        apiDataRepository.getZpList(null, new ApiNetResponse<List<ZpBean>>() {
            @Override
            public void onSuccess(List<ZpBean> data) {

                zp.setText(data.size()+"");
            }
        });
    }

    private void set(){
        Map<String,String> map = new HashMap<>();
        map.put("stime",TimeUtil.dateToStamp(time1.getText().toString()));
        map.put("etime",TimeUtil.dateToStamp(time2.getText().toString()));
        if(mknum.equals("3")){
            map.put("full",mzBean.getGoods().size()+"");
        }else {
            map.put("full",mknum);
        }
        map.put("goods",goodid.substring(0,goodid.length()-1));
        map.put("gift_id",zpid.substring(0,goodid.length()-1));
        apiDataRepository.mzset(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("设置成功");
                finish();
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 4:
                if (data != null) {
                    goodid = data.getStringExtra("id");
//                    goodlin.setVisibility(View.VISIBLE);
//                    selectgood.setVisibility(View.GONE);
//                    name.setText(data.getStringExtra("name"));
//                    price.setText("￥" + data.getStringExtra("price"));
//                    GlideUtil.showImg(getContext(), data.getStringExtra("pic"), gimg);
                }
                break;
            case 3:
                if (data != null) {
                    zpid = data.getStringExtra("id");
//                    goodlin.setVisibility(View.VISIBLE);
//                    selectgood.setVisibility(View.GONE);
//                    name.setText(data.getStringExtra("name"));
//                    price.setText("￥" + data.getStringExtra("price"));
//                    GlideUtil.showImg(getContext(), data.getStringExtra("pic"), gimg);
                }
                break;
            default:
                break;
        }
    }

}
